#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk
import pyCan

class DashboardDialog(object):

    def __init__(self):
        
        self.canObject = pyCan.pyCan("usb1")
        
        # create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_border_width(10)
    
        # box
        self.box = gtk.VBox(False, 0)
        self.window.add(self.box)
        
        self.boxSetting = gtk.HBox(False, 0)
        self.boxSetting.show()
        self.box.pack_start(self.boxSetting)
        
        self.boxOutput = gtk.VBox(False, 10)
        self.boxOutput.show()
        self.box.pack_start(self.boxOutput)
        
        # Creates a new label
        self.editableInput = gtk.Entry(max=0)
        self.editableInput.set_text(self.canObject.source)
        self.editableInput.show()
        self.boxSetting.pack_start(self.editableInput, True, True, 0)    
    
        # Creates a new button with the label "Hello World".
        self.buttonSetInput = gtk.Button("Set input")
        self.buttonSetInput.connect("clicked", self.buttonSetInputClick, None)    
        self.buttonSetInput.show()
        self.boxSetting.pack_start(self.buttonSetInput, True, True, 0)
        
        # Creates an output window
        self.sw = gtk.ScrolledWindow()
        self.sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.textview = gtk.TextView()
        self.textbuffer = self.textview.get_buffer()
        self.sw.add(self.textview)
        self.sw.show()
        self.textview.show()

        self.boxOutput.pack_start(self.sw, True, True, 0)
        
        # The final step is to display this newly created widget.
        self.box.show()
        self.window.show()

    def main(self):
        gtk.main()
        
    def buttonSetInputClick(self, widget, data=None):
        self.canObject.source = self.editableInput.get_text()
        self.textbuffer.set_text(self.canObject.read())   

if __name__ == "__main__":
    diag = DashboardDialog()
    diag.main()